package com.example.medicinetime;

import android.content.DialogInterface;
import android.os.Build;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;

import androidx.annotation.NonNull;
import androidx.annotation.RequiresApi;
import androidx.appcompat.app.AlertDialog;
import androidx.recyclerview.widget.RecyclerView;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.time.LocalDate;
import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;
import java.util.Calendar;
import java.util.Date;
import java.util.List;

//https://blog.csdn.net/Amanduzhuojiang/article/details/78339355
public class MyAdapter extends RecyclerView.Adapter<MyAdapter.ViewHolder>{
    public static final String TAG="MyAdapter";
    private List<MedicienRecord> list;
//    private OnLongClickListener mLongClickListener;
    public MyAdapter(List<MedicienRecord> list) {
        this.list = list;
    }



    //新建点击事件接口
    public interface OnItemClickLitener {
        void onItemClick(View view, int position);
    }

//    public interface OnLongClickListener{
//        boolean onLongClick(int position);
//    }


    private OnItemClickLitener mOnItemClickLitener;

    public void setOnItemClickLitener(OnItemClickLitener mOnItemClickLitener) {
        this.mOnItemClickLitener = mOnItemClickLitener;
    }

//    public void setLongClickListener(OnLongClickListener longClickListener) {
//        this.mLongClickListener = mLongClickListener;
//    }


    @NonNull
    @Override
    public MyAdapter.ViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
        View view = LayoutInflater.from(parent.getContext()).inflate(R.layout.carditem, parent, false);
        return new ViewHolder(view);
    }

    @RequiresApi(api = Build.VERSION_CODES.O)
    @Override
    public void onBindViewHolder(@NonNull MyAdapter.ViewHolder holder, int position) {
        holder.tv_card_time.setText(list.get(holder.getAdapterPosition()).getDailytime());
        holder.tv_card_date.setText(list.get(holder.getAdapterPosition()).getStartTime());
        holder.tv_card_medicien_name.setText(list.get(holder.getAdapterPosition()).getMedicienName());
        holder.tv_card_medicien_dosis.setText(" X "+String.valueOf(list.get(holder.getAdapterPosition()).getDosis()));
        holder.tv_id_invisible.setText(String.valueOf(list.get(holder.getAdapterPosition()).getId()));
        Date date;
        //获取系统当前时间
        Date now = Calendar.getInstance().getTime();
        //自定义时间格式化
        SimpleDateFormat sdf =   new SimpleDateFormat( "yyyy-MM-dd HH:mm" );
        String start_str=String.valueOf(list.get(holder.getAdapterPosition()).getStartTime());
        String dayly_str=String.valueOf(list.get(holder.getAdapterPosition()).getDailytime());
        String date_time = start_str+" "+dayly_str;
        Log.i(TAG, "onBindViewHolder: date_time="+date_time);
        try {
            date = sdf.parse( date_time );
            Log.i(TAG, "onBindViewHolder: date = "+date);
            if(now.after(date)){//超时
                Log.i(TAG, "onBindViewHolder: 超时");
                holder.tv_time_reminder.setVisibility(View.VISIBLE);
            }else{//未到时间
                Log.i(TAG, "onBindViewHolder: 未超时");
                holder.tv_time_reminder.setVisibility(View.INVISIBLE);
            }
        } catch (ParseException e) {
            e.printStackTrace();
            Log.i(TAG, "onBindViewHolder: catch");
        }

        final int postions = holder.getAdapterPosition();
        if (mOnItemClickLitener!=null){
            holder.itemView.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    int pos=holder.getAdapterPosition();
                    Log.i(TAG, "onClick: getAdapterPosition()="+holder.getAdapterPosition());
                    mOnItemClickLitener.onItemClick(holder.itemView,pos);

//                    removeData(position);
                }
            });
        }
//        if (mLongClickListener != null){
//            holder.itemView.setOnLongClickListener(new View.OnLongClickListener() {
//                @Override
//                public boolean onLongClick(View view) {
//                    Log.i(TAG, "onLongClick: 长按事件");
//                    int pos=holder.getAdapterPosition();
//                    return mLongClickListener.onLongClick(pos);
//                }
//            });
//        }
    }

    @Override
    public int getItemCount() {
        return list.size();
    }

    //  删除数据
    //  https://blog.csdn.net/ldq13026876956/article/details/107631946
    public void removeData(int position) {
        list.remove(position);
        Log.i(TAG, "removeData: position="+position);
        //删除
        notifyItemRemoved(position);
        notifyItemRangeChanged(position, list.size() - position);
    }




    public static class ViewHolder extends RecyclerView.ViewHolder {
        public TextView tv_card_time;
        public TextView tv_card_date;
        public TextView tv_card_medicien_name;
        public TextView tv_card_medicien_dosis;
        public TextView tv_time_reminder;
        public TextView tv_id_invisible;

        public ViewHolder(View itemView) {
            super(itemView);
            tv_card_time = (TextView) itemView.findViewById(R.id.card_time);
            tv_card_date = (TextView) itemView.findViewById(R.id.card_date);
            tv_card_medicien_name = (TextView) itemView.findViewById(R.id.card_medicien_name);
            tv_card_medicien_dosis = (TextView) itemView.findViewById(R.id.card_medicien_dosis);
            tv_time_reminder = (TextView) itemView.findViewById(R.id.time_reminder);
            tv_id_invisible = (TextView) itemView.findViewById(R.id.id_invisible);
        }
    }
}
